package com.sansui.admin.core.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sansui.admin.common.model.Result;
import com.sansui.admin.core.dao.RoleMapper;
import com.sansui.admin.core.dao.RoleResourcesMapper;
import com.sansui.admin.core.model.Resources;
import com.sansui.admin.core.model.Role;
import com.sansui.admin.core.model.RoleAddModel;
import com.sansui.admin.core.model.RolePage;
import com.sansui.admin.core.model.RoleResources;
import com.sansui.admin.core.model.UserRole;
import com.sansui.admin.core.service.RoleService;
import com.sansui.admin.core.service.UserService;
import com.sansui.admin.core.vo.RoleVO;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private UserService userService;
	@Autowired
	private RoleResourcesMapper roleResourcesMapper;

	@Override
	public Result insertRole(RoleAddModel role) {
		String[] resourcesId = role.getResourcesId();
		role.setId(UUID.randomUUID().toString());
		roleMapper.insert(new Role(role.getId(),role.getName()));
		if (resourcesId == null) {
			return new Result("success", null);
		}
		for (int i = 0; i < resourcesId.length; i++) {
			roleResourcesMapper.insert(new RoleResources(UUID.randomUUID().toString(), resourcesId[i], role.getId()));
		}
		return new Result("success", null);
	}

	@Override
	public Result updateRole(RoleAddModel role) {
		String[] resourcesId = role.getResourcesId();
		roleResourcesMapper.deleteByT(new RoleResources[] { new RoleResources(null, null, role.getId()) });
		roleMapper.updateByPrimaryKeyRoleAddModel(role);
		if (resourcesId == null) {
			return new Result("success", null);
		}
		for (int i = 0; i < resourcesId.length; i++) {
			roleResourcesMapper.insert(new RoleResources(UUID.randomUUID().toString(), resourcesId[i], role.getId()));
		}
		return new Result("success", null);
	}

	@Override
	public Result deleteRole(String[] id) {
		RoleResources[] r = new RoleResources[id.length];
		UserRole[] u = new UserRole[id.length];
		for (int i = 0; i < id.length; i++) {
			r[i] = new RoleResources(null, null, id[i]);
			u[i] = new UserRole(null, null, id[i]);
		}

		userService.deleteByT(u);
		roleResourcesMapper.deleteByT(r);
		roleMapper.deleteBy(id);
		return new Result("success", null);
	}

	@Override
	public RoleVO selectByPrimaryKey(String id) {
		RoleVO role = roleMapper.selectRoleVOByPrimaryKey(id);
		role.setRoleResourcesList(roleResourcesMapper.select(new RoleResources(null, null, id)));
		return role;
	}

	@Override
	public List<Role> selectAll() {
		return roleMapper.selectAll();
	}

	@Override
	public void deleteByT(RoleResources[] r) {
		roleResourcesMapper.deleteByT(r);
	}

	@Override
	public List<Resources> findResourcesByT(Resources resources) {
		return roleResourcesMapper.findResourcesByT(resources);
	}

	/* (non-Javadoc)  
	
	 * <p>Title: findTByPage</p>  
	
	 * <p>Description: </p>  
	
	 * @param role
	 * @return  
	
	 * @see com.sansui.admin.core.service.RoleService#findTByPage(com.sansui.admin.core.model.RolePage)  
	
	 */
	@Override
	public Object findTByPage(RolePage role) {
		// TODO Auto-generated method stub
		return this.roleMapper.selectByExample(role);
	}
}
